﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using Microsoft.IdentityModel.Claims;

namespace Veracruz.Facebook.Sharepoint.Webparts
{
    public sealed class VeracruzPeoplePicker
        :WebPart
    {
        private PeopleEditor objEditor;
        private Label lblAccount;
        private Button btnPostMe;

        public VeracruzPeoplePicker()
        {

        }

        protected override void CreateChildControls()
        {
            lblAccount = new Label();
            lblAccount.ID = "lblAccount";
            Controls.Add(lblAccount);

            objEditor = new PeopleEditor();
            objEditor.AutoPostBack = true;
            objEditor.PlaceButtonsUnderEntityEditor = true;
            objEditor.ID = "pplEditor";
            objEditor.AllowEmpty = false;
            objEditor.SelectionSet = "User,DL,SecGroup,SPGroup";
            objEditor.MultiSelect = false;
            Controls.Add(objEditor);

            btnPostMe = new Button();
            btnPostMe.Text = " OK ";
            btnPostMe.Click += new EventHandler(btnPostMe_Click);
            Controls.Add(btnPostMe);
        }

        private string GetAccountName()
        {
            if (ClaimsPrincipal.Current.Identities.Count > 0 && ClaimsPrincipal.Current.Identities[0].IsAuthenticated)
            {
                return ClaimsPrincipal.Current.Identities[0].Name;
            }
            else
            {
                return "Not authenticated yet";
            }
        }

        private void btnPostMe_Click(object sender, EventArgs eventArgs)
        {
            string strAccountName = GetAccountName();
            lblAccount.Text = "account name: " + strAccountName;
        }
    }
}
